package com.bwjf.dzfp.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.WebUtils;

import com.bwjf.dzfp.entity.User;

public class SessionTimeoutInterceptor extends HandlerInterceptorAdapter {

	public List<String> allowUrls;


	/**
	 * Session超时，拦截访问
	 * 
	 */
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String projectName=request.getContextPath();
		String requestUrl = request.getRequestURI();
		requestUrl=requestUrl.replaceFirst(projectName, "");
		for(String url : allowUrls) {
			if(url.endsWith("/**")){
				String u=url.substring(0,url.length()-3)+".htm";
				String u1=url.substring(0,url.length()-2);
				if(requestUrl.equals(u)||requestUrl.startsWith(u1)){
					return true;
				}
			}else if(requestUrl.startsWith(url)) {
				return true;
			}
		}
		
		User session = (User) WebUtils.getSessionAttribute(request,"user");
		//System.out.println("-------------------------------"+session);
		if(session != null) {
			return true;
		}else {
			response.sendRedirect(request.getContextPath());  
			return false;
		}
	}

	public List<String> getAllowUrls() {
		return allowUrls;
	}

	public void setAllowUrls(List<String> allowUrls) {
		this.allowUrls = allowUrls;
	}
}
